import CharactorStat from "../classes/CharactorStat";
import { Gifts, PlayerStat } from "../userData/UserModel";


const { ccclass, property } = cc._decorator;

@ccclass
export class Tool extends cc.Component {


    static getRandomNum(min = 0, max = 100) {
        return Math.floor(Math.random() * (max - min)) + min
    }

    getArrayDiffrence(a, b) {
        return a.filter(x => !b.includes(x));
    }

    public static getArrayHasInterset(a,b){
        return a.filter(x=>b.includes(x));
       
    }

    public static calDistance(x1: number, y1: number,x2: number, y2: number){
        return Math.abs(Math.sqrt( (x1-x2)* (x1-x2)+ (y1-y2)* (y1-y2))) 
    }


    public static fguiToWorld(x: number, y: number, item: fgui.GObject) {
        //GObject.node.convertToWorldSpaceAR(cc.Vec2.ZERO)
        let screenPos = item.localToGlobal(x, y);
        //原点位置转换
        screenPos.y = cc.winSize.height - screenPos.y - 200;
        return cc.v2(cc.Camera.main.getScreenToWorldPoint(screenPos));
    }


    public static worldtofgui(x: number, y: number) {
        //GObject.node.convertToWorldSpaceAR(cc.Vec2.ZERO)
        let screenPos = cc.Camera.main.getWorldToScreenPoint(cc.v2(x, y));
        //原点位置转换
        screenPos.x = screenPos.x + cc.winSize.width / 2 - 100;
        screenPos.y = cc.winSize.height + screenPos.y - 300
        return cc.v2(fgui.GRoot.inst.globalToLocal(screenPos.x, screenPos.y
        ));
    }

    public static worldtofgui1(x: number, y: number) {
        let screenPos = cc.Camera.main.getWorldToScreenPoint(cc.v2(x, y));
        
        screenPos.y = cc.winSize.height - screenPos.y 
        return cc.v2(fgui.GRoot.inst.globalToLocal(screenPos.x, screenPos.y
        ));
    }

    public static rtrim(str: string, delimeter: string) {
        return (str.substring(str.length - 1) == delimeter) ? str.substring(0, str.length - 1) : str;
    }

    static clamp(value, min, max):number {
      // 如果value小于min，则返回min；如果大于max，则返回max；否则返回value本身
      return Math.max(min, Math.min(max, value));
  }

    public static caculatePlayerStat(id:number,level:number,gifts){
        let stat = PlayerStat[id-1];
    
        let s= new Object();
        
        s.hp = Math.round(8 * 5 * Math.pow(1.2, level - 1))
        let maxHp = Math.round(PlayerStat[id - 1].hp * Math.pow(1.2, level - 1));
        s.atk = Math.round(stat.hp / 1.5 / 3);
        s.maxExp = Math.round(8 * 5 * Math.pow(1.2, level - 1))*2;
        s.hp = maxHp;
        stat.giftIds.forEach((v)=>{
            
            if (gifts[id][v]){
                 let gift = Gifts[v-1];
                 if(gift.type == "statUp"){
                     s[gift.statName]+=gift.value;
                 }
            }
        });

        return s;
        
    }

    static shuffleArray(arr){
        arr.sort(function() {
            return (0.5-Math.random());
        });
        return arr;
    }

    static getPointsWithinDistance(x, y, distance) {
        const points = [];
        
        for (let i = -distance; i <= distance; i++) {
          for (let j = -distance; j <= distance; j++) {
            const dx = Math.abs(i);
            const dy = Math.abs(j);
            const pointDistance = Math.sqrt(dx * dx + dy * dy);
            
            if (pointDistance === distance) {
              points.push({ x: x + i, y: y + j });
            }
          }
        }
        
        return points;
      }

      static getValidMovePoints(x, y, range,occupiedPoints) {
        const validPoints = [];
      
        for (let i = -range; i <= range; i++) {
          for (let j = -range; j <= range; j++) {
            const dx = Math.abs(i);
            const dy = Math.abs(j);
            const distance = Math.sqrt(dx * dx + dy * dy);
      
            if (distance <= range && !occupiedPoints.some(p => p.x === x + i && p.y === y + j)) {
              validPoints.push({ x: x + i, y: y + j });
            }
          }
        }
      
        return validPoints;
      }
    
}
